<?php
//all the class/function includes are managed in the includer file
include("include/includer.php");

//get all the configuration variables (custom function in newzbook.php include)
getConfigVars();  

/*----------------------*\
 |  Setup the variables |
\*----------------------*/
//GET
if(isset($_GET['mode'])){
	$mode = $_GET['mode'];
}
if(isset($_GET['value'])){
	$value = $_GET['value'];
}
if(isset($_GET['value2'])){
	$value2 = $_GET['value2'];
}

//create the base url
$url = $config_sabnzbd_url."api?apikey=".$config_sabnzbd_apikey;

/*------------------------------*\
 |  Functions for various modes |
\*------------------------------*/
//Get the summary data
if($mode == 'refreshmain'){
	$url = $url."&mode=queue&output=json";
}
//history
if($mode =='history'){
	$url = $url."&mode=history&output=json";
}
//Pause
if($mode == 'pause'){
	$url = $url."&mode=pause";
}
//Resume
if($mode == 'resume'){
	$url = $url."&mode=resume";
}

//Set Speed
if($mode == 'setspeed'){
	$url = $url."&mode=config&name=speedlimit&value=".$value;
}

//Per Item
//delete
if($mode == 'deleteitem'){
	$url = $url.'&mode=queue&name=delete&value='.$value;
}
//move
if($mode == 'moveitem'){
	$url = $url.'&mode=switch&value='.$value.'&value2='.$value2;
}
//pause
if($mode == 'pauseitem'){
	$url = $url.'&mode=queue&name=pause&value='.$value;
}
//resume
if($mode == 'resumeitem'){
	$url = $url.'&mode=queue&name=resume&value='.$value;
}
//change category (value=nzbid, value2=category)
if($mode == 'changecategory'){
	$url = $url.'&mode=change_cat&value='.$value.'&value2='.$value2;
}
//change item name (value nzbid, value2 name)
if($mode == 'changename'){
	$url = $url.'&mode=queue&name=rename&value='.$value.'&value2='.$value2;
}
//Delete History Item
if($mode == 'deletehistory'){
	$url = $url.'&mode=history&name=delete&value='.$value;
}
//change post processing
/*
URL: api?mode=change_opts&value=SABnzbd_nzo_zt2syz&value2=0
Description: Change value2 to control what post-processing option to use
Skip: 0
+Repair: 1
+Repair/Unpack: 2
+Repair/Unpack/Delete: 3
*/

//change priority
/*
api?mode=queue&name=priority&value=SABnzbd_nzo_zt2syz&value2=0
Description:
Low Priority: -1
Normal Priority: 0
High Priority: 1
Will return the new position in the queue if successful.
*/
if(isset($url)){
echo file_get_contents($url);
}
else{
	echo "ERROR: Something was wrong with the request. URL was not ever formed.";
}
?>